﻿using AnlianCommon.Common.Domains.Addresses;
using AnlianCommon.Common.Domains.SheepFarmHouse;
using AnlianCommon.Common.Domains.OrganizationalUnits;
using AnlianCommon.Common.Domains.UserPermissions;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.SheepFarmes
{
    public class SheepFarmsQueryHandler : IRequestHandler<SheepFarmsQueryCommand, ResultPage<SheepFarmsDto>>
    {
        private readonly IBaseRepository<SheepFarm> _sheepFarmRepository;
        private readonly IBaseRepository<Province> _provinceRepository;
        private readonly IBaseRepository<Directly> _directlyRepository;
        private readonly IBaseRepository<County> _countyRepository;
        private readonly IMapper _mapper;

        public SheepFarmsQueryHandler(IBaseRepository<SheepFarm> sheepFarmRepository, IBaseRepository<Province> provinceRepository, IBaseRepository<Directly> directlyRepository, IBaseRepository<County> countyRepository, IMapper mapper)
        {
            _sheepFarmRepository = sheepFarmRepository;
            _provinceRepository = provinceRepository;
            _directlyRepository = directlyRepository;
            _countyRepository = countyRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SheepFarmsDto>> Handle(SheepFarmsQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SheepFarmsDto>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var sheepFarms = await (from a in await _sheepFarmRepository.GetAll()
                                        join b in await _provinceRepository.GetAll() on a.ProvinceId equals b.ProvinceId
                                        join c in await _directlyRepository.GetAll() on a.DirectlyId equals c.DirectlyId
                                        join d in await _countyRepository.GetAll() on a.CountyId equals d.CountyId
                                        select new SheepFarmsDto()
                                        {
                                            FarmId = a.FarmId,
                                            FarmNumber = a.FarmNumber,
                                            FarmFullName = a.FarmFullName,
                                            FarmShortName = a.FarmShortName,
                                            FarmSize = a.FarmSize,
                                            ProvinceId = a.ProvinceId,
                                            ProvinceName = b.ProvinceName,
                                            FarmDetailedAddress = a.FarmDetailedAddress,
                                            FarmCreateTime = a.FarmCreateTime,
                                            PersId = a.PersId,
                                            DepartmentId = a.DepartmentId,
                                            FormState = a.FormState,
                                            DirectlyId = c.DirectlyId,
                                            DirectlyName = c.DirectlyName,
                                            CountyId = d.CountyId,
                                            CountyName = d.CountyName,
                                        }).ToListAsync();
                var list = _mapper.Map<List<SheepFarmsDto>>(sheepFarms);
                if (!string.IsNullOrEmpty(request.FarmFullName))
                {
                    list = list.Where(c => c.FarmFullName.Contains(request.FarmFullName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.PersId))
                {
                    list = list.Where(c => c.PersId == request.PersId).ToList();
                }
                result.TotalCount = list.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                list = list.OrderBy(c => c.FarmId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = list;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
